<?php
namespace Home\Controller;

class MyhomeController extends AdminController
{
	public function __construct(){
		parent::__construct();
		// $this->model = new Model('user');
	}
	public function index(){
		$id=session('admin_user')['id'];
		$bookrack=M('Bookrack')->where("uid=$id")->select();
		$arr = array(); //声明一个空数组
		foreach ($bookrack as $v1) {
			$book = M('Book')->where(array('id'=>array('eq',$v1['bookid'])))->select();
			// var_dump($book);exit;
			$books = array();
			foreach($book as $v2){
				$books[] =  $v2;			
			}

			$user=M('User')->where(array('id'=>array('eq',$v1['uid'])))->select();
			$users=array();
			foreach ($user as $v3) {
				$users[] = $v3;
			}

			$v1['book'] = $books;
			$v1['user'] = $users; //将新得到角色信息放置到$v中
			$arr[] = $v1;
		}
		
		// var_dump($arr);exit;
		$this->assign('list',$arr);



		$this->assign("title","乐阅网");
		$this->display('Myhome/index');
	}

	//加载编辑页面
	public function edit(){
		$this->assign("title","乐阅网");
		$this->display('Myhome/edit');
	}

	public function update(){

		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Public/home/images/'; // 设置附件上传根目录
		$upload->savePath = ''; // 设置附件上传（子）目录
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
		$this->error($upload->getError());
		}else{// 上传成功
		$this->success('上传成功！');
		}

		// 取得成功上传的文件信息
		$info = $upload->upload();
		$data['img'] = $info['img']['savename'];
		//生成数据对象	
		$model = M('user')->create();
		if ($model ->save($data) > 0 ) {
			$this->success('修改成功',U('Myhome/index') );
		} else {
			$this->error('修改失败');
		}				
	}



}